package com.distribute.application.admin;

import com.distribute.framework.core.web.helper.Utils;
import java.util.List;
import com.distribute.framework.model.*;
import com.distribute.framework.plugin.*;


public class LoginHelper {

	public static final String PRIVATEKEY = "ae7dc7ca27fbd95608afb98e33f26ab1";

	public static SrvUser getUser(){
		return Utils.getSession("distribute_user");
	}
	
	public static void setUser(SrvUser srvUser){
		Utils.setSession("distribute_user", srvUser);
		if(srvUser==null){
			Utils.setSession("distribute_user_id", null);
		}else{
			Utils.setSession("distribute_user_id", srvUser.getSuId());
		}
	}
	
	public static Long getUserId(){
		return (Long) Utils.getSession("distribute_user_id");		
	}
	
	public static String getGroupLike(SrvUser srvUser) {
		if(srvUser.getSuId().longValue()==0)return null;
		SrvGroup srvGroup = SrvGroup.where.sgUser(srvUser.getId()).get();
		return (srvGroup==null?null:srvGroup.getSgFamily());
	}

	public static Object getGroupLike() {
		return getGroupLike(LoginHelper.getUser());
	}

	public static Long getGroupRoot() {
		if(LoginHelper.getUserId().longValue()==0)
			return 0l;
		SrvGroup srvGroup = SrvGroup.where.sgUser(LoginHelper.getUserId()).get();
		if(srvGroup!=null){
			return srvGroup.getId();
		}else{
			return -1l;
		}
	}

	
	public static void setUSID(String uid, String sid) {
		Cache.defaultRedisSupport.setValue("USID", uid, sid);
	}
	
	
	public static Long inHost(Long hostid) {
		String strHosts = LoginHelper.getUser().getSuHost();
		if(hostid==null || Utils.empty(strHosts))
			return hostid;
		if(Utils.idin(strHosts, hostid))
			return hostid;
		return null;
	}
	
	public static String testHost(Long hostid) {
		String strHosts = LoginHelper.getUser().getSuHost();
		if(hostid==null)
			return strHosts==null?null:strHosts.replace(" ", "").replace("#", "");
		if(Utils.empty(strHosts) || strHosts.equals("0"))
			return hostid.toString(); 
		for(String sId:strHosts.split(",")){
			if(Utils.trim(sId).equals(hostid.toString()))
				return hostid.toString();
		}
		return strHosts;
	}
	
	public static List<SrvHost> getHosts(){
		if(LoginHelper.getUser().getId().longValue()==0)
			return SrvHost.where.list(-1);
		String strHosts = LoginHelper.getUser().getSuHost();
		strHosts = strHosts.replace(" ", "").replace("#", "");
		return SrvHost.where.shIdIn(strHosts).list(-1);
	}
	
	public static SrvHost getAdminHost() {
		return SrvHost.get(getAdminHostId());
	}
	public static Long defaultAdminHostId=null;
	public static Long getAdminHostId() {
		if(defaultAdminHostId==null){
			SrvHost defaultSrvHost = SrvHost.where.get();
			if(defaultSrvHost!=null){
				defaultAdminHostId = defaultSrvHost.getId();
			}
		}
		Long session_host_id = Utils.getSession("session_like_host");
		if(session_host_id!=null){
			SrvHost srvHost = SrvHost.get(session_host_id);
			if(srvHost!=null)
				return srvHost.getId();
		}
		if(LoginHelper.getUser()==null){
			return defaultAdminHostId;
		}
		String strHosts = LoginHelper.getUser().getSuHost();
		if(!Utils.empty(strHosts)){
			Long hostid = Long.parseLong(strHosts.replace("#0#,", "").replace("#", "").replace(" ", "").split(",")[0]);
			return hostid;
		}
		return defaultAdminHostId;
	}
	

	public static String getUser$() {
		// TODO Auto-generated method stub
		return LoginHelper.getUser().getKeyValue();
	}

	public static Object getUserDroit(Class<SrvUser> class1) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static SrvUser getUser(Class<SrvUser> class1) {
		// TODO Auto-generated method stub
		return null;
	}
	/************CTRL_DEFINE_CODE************/
	/************CTRL_DEFINE_CODE************/
	
}